#!/bin/bash

# supercop/data-init
# D. J. Bernstein
# Public domain.

. ./data-vars

if [ -d supercop-data ]
then
  mkdir -p "$top"
else
  if ln -s ../supercop-data supercop-data
  then
    mkdir -p ../supercop-data
  else
    mkdir -p supercop-data
  fi
  mkdir -p "$top"
fi

exec </dev/null
exec >"$top/log"
exec 2>&1
exec 6>"$top/$abi/todo"

echo "=== `date` === starting data-init"

echo "=== `date` === building machineinfo"
rm -rf "$top/machineinfo"
mkdir -p "$top/machineinfo"
cp -pr machineinfo/* "$top/machineinfo"
( cd "$top/machineinfo" && sh do )

echo "=== `date` === building scripts"
rm -rf "$top/scripts"
mkdir -p "$top/scripts"
cp -pr scripts/* "$top/scripts"

echo "=== `date` === building okcompilers"
rm -rf "$top/okcompilers"
mkdir -p "$top/okcompilers"
cp -pr okcompilers/* "$top/okcompilers"
( cd "$top/okcompilers" && sh do )

echo "=== `date` === building basic dependencies"
okabi \
| while read abi
do
  mkdir -p "$top/$abi"
  mkdir -p "$top/$abi/bin"
  mkdir -p "$top/$abi/lib"
  mkdir -p "$top/$abi/include"
  echo "=== `date` === cleaning up BUILD in $abi"
  find "$top/$abi" -name BUILD | xargs rm -f
  echo "=== `date` === cleaning up LOCKED in $abi"
  find "$top/$abi" -name LOCKED | xargs rm -f
  echo "=== `date` === cleaning up LOCK in $abi"
  find "$top/$abi" -name LOCK | xargs rm -f

  mkdir -p "$top/$abi/base"
  touch "$top/$abi/base/BUILD"
  touch "$top/$abi/base/LOCK"
  : > "$top/$abi/base/dep"
  echo "$abi/base" >&6

#  mkdir -p "$top/$abi/cryptopp"
#  touch "$top/$abi/cryptopp/BUILD"
#  touch "$top/$abi/cryptopp/LOCK"
#  : > "$top/$abi/cryptopp/dep"
#  echo "$abi/cryptopp" >&6

#  mkdir -p "$top/$abi/openssl"
#  touch "$top/$abi/openssl/BUILD"
#  touch "$top/$abi/openssl/LOCK"
#  : > "$top/$abi/openssl/dep"
#  echo "$abi/openssl" >&6

#  mkdir -p "$top/$abi/gmp"
#  touch "$top/$abi/gmp/BUILD"
#  touch "$top/$abi/gmp/LOCK"
#  : > "$top/$abi/gmp/dep"
#  echo "$abi/gmp" >&6
done


echo "=== `date` === building killafter"
rm -rf "$top/killafter"
mkdir -p "$top/killafter"
cp -pr killafter/* "$top/killafter"
( cd "$top/killafter" && sh do )

echo "=== `date` === building sinceepoch"
rm -rf "$top/sinceepoch"
mkdir -p "$top/sinceepoch"
cp -pr sinceepoch/* "$top/sinceepoch"
( cd "$top/sinceepoch" && sh do )

echo "=== `date` === building checksum"
rm -rf "$top/checksum"
mkdir -p "$top/checksum"
cp -pr checksum/* "$top/checksum"
( cd "$top/checksum" && sh do )

echo "=== `date` === building baseinclude"
okabi \
| while read abi
do
  cp -p include/* "$top/$abi/include/"
done

echo "=== `date` === building cpuid"
rm -rf "$top/cpuid"
mkdir -p "$top/cpuid"
cp -pr cpuid/* "$top/cpuid"
( cd "$top/cpuid" && sh do )
okabi \
| while read abi
do
  cp -p "$top/cpuid/include"/* "$top/$abi/include/"
done

echo "=== `date` === building inttypes"
rm -rf "$top/inttypes"
mkdir -p "$top/inttypes"
cp -pr inttypes/* "$top/inttypes"
( cd "$top/inttypes" && sh do )
okabi \
| while read abi
do
  cp -p "$top/inttypes/include/$abi"/* "$top/$abi/include/"
done

echo "=== `date` === building cpucycles"
rm -rf "$top/cpucycles"
mkdir -p "$top/cpucycles"
cp -pr cpucycles/* "$top/cpucycles"
( cd "$top/cpucycles" && sh do )
okabi \
| while read abi
do
  cp -p "$top/cpucycles/include/$abi"/* "$top/$abi/include/"
  cp -p "$top/cpucycles/lib/$abi"/* "$top/$abi/lib/"
done


okabi \
| while read abi
do
  for language in c cpp
  do
    ok${language}-$abi \
    | while read compiler
    do
      compilerword=`echo "$compiler" | tr ' ' '_'`
      echo "=== `date` === todo $abi/compiler/$language/$compilerword"
      echo "$abi/compiler/$language/$compilerword" >&6
      mkdir -p "$top/$abi/compiler/$language/$compilerword"
      touch "$top/$abi/compiler/$language/$compilerword/BUILD"
      touch "$top/$abi/compiler/$language/$compilerword/LOCK"
      : > "$top/$abi/compiler/$language/$compilerword/dep"
    done
  done
done


cat OPERATIONS \
| while read o macros prototypes
do
  [ -d "$o" ] || continue

  ls "$o" \
  | sort \
  | while read p
  do
    [ -d "$o/$p" ] || continue
    okabi \
    | while read abi
    do
      find "$o/$p" -follow -name "api.h" \
      | sort \
      | while read doth
      do
	implementationdir=`dirname $doth`
	opi=`echo "$implementationdir" | tr ./- !!!`

	if [ -f "$implementationdir/architectures" ]
	then
	  if grep '^'"$abi"'$' "$implementationdir/architectures" >/dev/null
	  then
	    :
	  else
            echo "=== `date` === skipping $abi/implementation/$implementationdir"
	    continue
	  fi
	fi

        echo "=== `date` === todo $abi/implementation/$implementationdir"
        echo "$abi/implementation/$implementationdir" >&6
        mkdir -p "$top/$abi/implementation/$implementationdir"
        touch "$top/$abi/implementation/$implementationdir/BUILD"
        touch "$top/$abi/implementation/$implementationdir/LOCK"

        (
          cat "$o/try.c"
          cat "$o/measure.c"
          cat "try-anything.c"
          cat "measure-anything.c"
          find "$implementationdir" -follow -type f \
	  | egrep '\.c$|\.s$|\.S$|\.cc$|\.cpp$' \
          | xargs cat
        ) \
        | grep '#include' \
        | sed 's/#include[      ]*//' \
        | sed 's_/.*/__' \
        | sed 's_//.*__' \
        | tr -d '<>"\040\011\014' \
        | sed 's_\.h$__' \
	| sort -u \
        | while read incl
        do
          case "$incl" in
#            openssl*)
#	      echo openssl
#              ;;
#            cryptopp*)
#	      echo cryptopp
#              ;;
#            gmp*)
#              echo gmp
#              ;;
            crypto_*_*)
              op=`echo "$incl" | sed 's/_\(.*\)_/_\1\//'`
	      if [ -d "$top/$abi/integrate/$op" ]
	      then
	        echo "integrate/$op"
	      fi
              ;;
          esac
        done | sort -u > "$top/$abi/implementation/$implementationdir/dep"

	implementationdir=`dirname $doth`
        echo "=== `date` === todo $abi/try/.../$implementationdir"
	opi=`echo "$implementationdir" | tr ./- !!!`

	cfiles=`ls "$implementationdir" | grep '\.c$' || :`
	sfiles=`ls "$implementationdir" | grep '\.[sS]$' || :`
	ccfiles=`ls "$implementationdir" | grep '\.cc$' || :`
	cppfiles=`ls "$implementationdir" | grep '\.cpp$' || :`

	language=c
	[ "x$cppfiles" = x ] || language=cpp
	[ "x$ccfiles" = x ] || language=cpp

	ok${language}-$abi \
	| while read compiler
	do
	  compilerword=`echo "$compiler" | tr ' ' '_'`
          mkdir -p "$top/$abi/try/$language/$compilerword/$implementationdir"
          touch "$top/$abi/try/$language/$compilerword/$implementationdir/BUILD"
          touch "$top/$abi/try/$language/$compilerword/$implementationdir/LOCK"
	  (
            echo "compiler/$language/$compilerword"
            echo "implementation/$implementationdir"
	  ) > "$top/$abi/try/$language/$compilerword/$implementationdir/dep"
          echo "$abi/try/$language/$compilerword/$implementationdir" >&6
	done

      done
    done

    okabi \
    | while read abi
    do
      echo "=== `date` === todo $abi/integrate/$o/$p"
      echo "$abi/integrate/$o/$p" >&6
      mkdir -p "$top/$abi/integrate/$o/$p"
      touch "$top/$abi/integrate/$o/$p/BUILD"
      touch "$top/$abi/integrate/$o/$p/LOCK"
      (
	echo base
        find "$o/$p" -follow -name "api.h" \
        | sort \
        | while read doth
        do
          implementationdir=`dirname $doth`
          for language in c
          do
            ok${language}-$abi \
            | while read compiler
            do
              compilerword=`echo "$compiler" | tr ' ' '_'`
              source="try/$language/$compilerword/$implementationdir"
              if [ -d "$top/$abi/$source" ]
              then
                echo "$source"
              fi
            done
          done
        done
      ) > "$top/$abi/integrate/$o/$p/dep"
    done
  done
done


cat OPERATIONS \
| while read o macros prototypes
do
  [ -d "$o" ] || continue
  for m in 1 2 3
  do
    ls "$o" \
    | sort \
    | while read p
    do
      [ -d "$o" ] || continue
      okabi \
      | while read abi
      do
        [ -d "$o/$p" ] || continue
        echo "=== `date` === todo $abi/measure$m/$o/$p"
        echo "$abi/measure$m/$o/$p" >&6
        mkdir -p "$top/$abi/measure$m/$o/$p"
        touch "$top/$abi/measure$m/$o/$p/BUILD"
        touch "$top/$abi/measure$m/$o/$p/LOCK"
        find "$o/$p" -follow -name "api.h" \
        | sort \
        | while read doth
        do
          implementationdir=`dirname $doth`
          for language in c cpp
          do
            ok${language}-$abi \
            | while read compiler
            do
              compilerword=`echo "$compiler" | tr ' ' '_'`
              source="try/$language/$compilerword/$implementationdir"
              if [ -d "$top/$abi/$source" ]
              then
                echo "$source"
              fi
            done
          done
        done > "$top/$abi/measure$m/$o/$p/dep"
      done
    done
  done
done


echo "=== `date` === ending data-init"
